//
//  HMSelectType.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/16.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMSelectType.h"
#import "HomeCategory.h"
#import "HMSelectTitleBtn.h"
#define heightMarin 30
#define widthMarin  11
#define widthBtn    (KWidthSCREEN - widthMarin * 5) / 4
#define heightBtn   60
#define btnFrame(location) CGRectMake((location % 4 + 1) * widthMarin + (location % 4) * widthBtn, (location / 4 + 1) * heightMarin + location / 4 * heightBtn, (KWidthSCREEN - widthMarin * 5) / 4, heightBtn)

@interface HMSelectType ()

NS_STRONG HMSelectTitleBtn *moreBtn;

NS_STRONG HMSelectTitleBtn *pickUpBtn;

NS_STRONG UIView *backView;

@end

@implementation HMSelectType

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray {
    self = [super init];

    if (self) {
        self.frame = CGRectMake(0, 64, KWidthSCREEN, KHeightSCREEN);
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.2];
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN, 0)];
        _backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_backView];
        _dataArray = dataArray;
        [self getPickDown];
    }
    return self;
}

- (void)getPickDown {
    [_backView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (_dataArray != nil) {
        if (_dataArray.count == 0) {
            _backView.frame = CGRectMake(0, 0, KWidthSCREEN, 0);
        } else {
            _backView.frame = CGRectMake(0, 0, KWidthSCREEN, (_dataArray.count / 4 + 1) * heightMarin + _dataArray.count / 4 *heightBtn);
        }
        if (_dataArray.count < 8) {
            for (int i = 0; i < _dataArray.count; i++) {
                HomeCategoryDetail *model = _dataArray[i];
                HMSelectTitleBtn *titleBtn = [[HMSelectTitleBtn alloc] initWithFrame:btnFrame(i) HomeCategory:model];
                titleBtn.selectTitle = ^(HomeCategoryDetail *cate) {
                    if ([_delegate respondsToSelector:@selector(select:)]) {
                        [_delegate select:cate];
                    }
                };
                [_backView addSubview:titleBtn];
            }
        } else {
            for (int i = 0; i < 7; i++) {
                HomeCategoryDetail *model = _dataArray[i];
                HMSelectTitleBtn *titleBtn = [[HMSelectTitleBtn alloc] initWithFrame:btnFrame(i) HomeCategory:model];
                titleBtn.selectTitle = ^(HomeCategoryDetail *cate) {
                    if ([_delegate respondsToSelector:@selector(select:)]) {
                        [_delegate select:cate];
                    }
                };
                [_backView addSubview:titleBtn];
            }
            
            HMSelectTitleBtn *moreBtn = [[HMSelectTitleBtn alloc] initWithFrame:btnFrame(7) imageURL:@"ProductList_fl-More" title:@"更多"];
            __weak typeof(self) weakself = self;
            moreBtn.dismiss = ^{
                [weakself changeSlectView];
            };
            [_backView addSubview:moreBtn];
        }
    }
}

- (void)changeSlectView {
    [_backView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _backView.frame = CGRectMake(0, 0, KWidthSCREEN, (_dataArray.count / 4 + 2) * heightMarin + (_dataArray.count / 4 + 1) *heightBtn);
    
    for (int i = 0; i < _dataArray.count; i++) {
        HomeCategoryDetail *model = _dataArray[i];
        HMSelectTitleBtn *titleBtn = [[HMSelectTitleBtn alloc] initWithFrame:btnFrame(i) HomeCategory:model];
        titleBtn.selectTitle = ^(HomeCategoryDetail *cate) {
            if ([_delegate respondsToSelector:@selector(select:)]) {
                [_delegate select:cate];
            }
        };
        [_backView addSubview:titleBtn];
    }
    _pickUpBtn = [[HMSelectTitleBtn alloc] initWithFrame:btnFrame(_dataArray.count) imageURL:@"ProductList_fl-Packup" title:@"收起"];
    __weak typeof(self) weakself = self;
    _pickUpBtn.dismiss = ^{
        [weakself getPickDown];
    };
    [_backView addSubview:_pickUpBtn];

    
}

@end
